package sort.simple;

/**
 * 冒泡排序
 * @author caasi
 * @since 2021-12-30 14:04:41
 */
public class BubbleSort {

    public static void main(String[] args) {
        int n = 1000000;
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            int r = (int) (Math.random() * 1000);
            arr[i] = r;
        }
        long start = System.currentTimeMillis();
        BubbleSort.sort(arr);
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start) + "ms");
    }

    public static void sort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
}
